package istar.impl;

import istar.IActor;
import istar.IDependency;
import istar.IElement;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class Actor implements IActor, Serializable, Cloneable{

	private String id, type, name;
	//private List<IElement> mainElements = new Vector<IElement>();
	private List<IElement> dependeeElements = new Vector<IElement>();
	private List<IElement> dependerElements = new Vector<IElement>();
	private List<IElement> inboundaryElements = new Vector<IElement>();
	//private List<IResource> resources = new Vector<IResource>();
	
	public Actor(){
		super();
	}
	
	public Actor(String _name){
		super();
		this.name = _name;
	}
	
	@Override
	public String getId() {
		return id;
	}
	
	@Override
	public void setId(String id) {
		this.id = id;
	}
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public List<IElement> getDependeeElements() {
		return dependeeElements;
	}

	@Override
	public List<IElement> getDependerElements() {
		return dependerElements;
	}

	@Override
	public List<IElement> getInboundaryElements() {
		return inboundaryElements;
	}
	
	@Override
	public void setInboundaryElements(List<IElement> inboundaryElements) {
		this.inboundaryElements = inboundaryElements;
	}

	public List<IElement> getMainElements() {
		List<IElement> mainElements = new Vector<IElement>();
		Iterator<IElement> itInternalElmnts = this.inboundaryElements.iterator();
		while (itInternalElmnts.hasNext()){
			IElement internalElmnt = itInternalElmnts.next();
			if (internalElmnt.isMain())
				mainElements.add(internalElmnt);
		}
		return mainElements;
	}


	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public void addInternalElement(IElement elmnt) {
		inboundaryElements.add(elmnt);
		elmnt.setOwner(this);
	}
	
	public IElement getInternalElement(String _elmntID){
		Iterator<IElement> itElmnts = inboundaryElements.iterator();
		while (itElmnts.hasNext()){
			IElement _elmnt = itElmnts.next();
			if (_elmnt.getId().equals(_elmntID))
				return _elmnt;
		}
		return null;
	}
	
	/*
	public IElement getInternalElement(String _name){
		Iterator<IElement> itElmnts = inboundaryElements.iterator();
		while (itElmnts.hasNext()){
			IElement _elmnt = itElmnts.next();
			if (_elmnt.getName().equals(_name))
				return _elmnt;
		}
		return null;
	}*/

	@Override
	public boolean able(IElement _elmnt) {
		if (this.getInternalElement(_elmnt.getId())==null){
			Iterator<IDependency> itDeps = _elmnt.getDependencies().iterator();
			while (itDeps.hasNext()){
				IDependency dep = itDeps.next();
				if (dep.getDependee().equals(this)){
					if (dep.getDependeeElement()!=null){
						if (this.able(dep.getDependeeElement()))
							return true;
					}
				}
			}
			return false;
		}
		else { 
			if (_elmnt.isBasic()) {
				return true;
			} 
			else {
				if (_elmnt.getMeansEndLink()==null)
					return false;
				else 
					return true;
			}
		}
	}	
	

	@Override
	public boolean workable(IElement elmnt) {
		return elmnt.workable(this);
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		Actor newClone = (Actor)super.clone();
		newClone.id = this.id;
		newClone.name = this.name;
		newClone.type = this.type;
		newClone.dependeeElements = new Vector<IElement>();
		newClone.dependerElements = new Vector<IElement>();
		newClone.inboundaryElements = new Vector<IElement>();
		
		try {
			Iterator<IElement> itMainElmnts = this.getMainElements().iterator();
			while (itMainElmnts.hasNext()){
				IElement mainElmnt = (IElement)itMainElmnts.next();
				IElement clone = (IElement)mainElmnt.clone(newClone);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		newClone.dependeeElements.addAll(this.dependeeElements);
		newClone.dependerElements.addAll(this.dependerElements);
		
		return newClone;
	}

	
	
	
	/*
	@Override
	public List<IResource> getResources() {
		return resources;
	}
	*/
	
}
